package com.tjetc.view;

import com.tjetc.proxy.CilentProxy;
import com.tjetc.view.adminview.AdminsMainView;
import com.tjetc.view.studentview.StudentMainView;
import com.tjetc.view.teacherview.TeacherMainView;
import com.zhiyun.domain.*;
import com.zhiyun.service.AdminService;
import com.zhiyun.service.RoleService;
import com.zhiyun.service.UserService;
import com.zhiyun.vo.AppData;
import com.zhiyun.vo.Result;

import java.util.List;
import java.util.Scanner;

public class MainView {
    RoleService roleService= CilentProxy.getProxyInstance(RoleService.class);
    UserService userService=CilentProxy.getProxyInstance(UserService.class);
    AdminService adminService=CilentProxy.getProxyInstance(AdminService.class);
     Scanner scanner=new Scanner(System.in);
    public void start() {
        while (true){
            System.out.println("~~~~~~~~~用户登录界面~~~~~~~~~");
            System.out.println("1.用户登录");
            System.out.println("2.用户注册");
            System.out.println("3.管理员登录");
            System.out.println("4.退出系统");
            int i = scanner.nextInt();
            switch (i){
                case 1:
                   userLogin();


                    break;
                case 2:
                    if (userRegister()){
                        System.out.println("注册成功");
                    }else {
                        System.out.println("注册失败");
                    }
                    break;
                case 3:
                    adminLogin();
                    break;
                case 4:
                    System.out.println("正在退出");
                    return;
                default:
                    break;
            }
        }
    }

    private  void adminLogin() {
        while (true){
            System.out.println("请输入用户名");
            String name = scanner.next();
            System.out.println("请输入密码");
            String password = scanner.next();
            Admin admin = new Admin();
            admin.setUsername(name);
            admin.setPassword(password);
            Result result = adminService.adminLogin(admin);
            if (result.getCode()==200){
                Admin obj = (Admin) result.getObj();
                AppData.admin=obj;
                new AdminsMainView().start();
                break;
            }else {
                System.out.println("用户名或Miami错误,请重新输入");
            }
        }
    }

    private  boolean userRegister() {
        while (true){
            int id;
            while (true){
                boolean a=false;
                Result all = roleService.findAll();
                List<Role> obj = (List<Role>) all.getObj();
                for (Role role : obj) {
                    System.out.println(role);
                }
                if (all.getCode()==200){
                    System.out.println("请输入想要创建的用户类型");
                    int i = scanner.nextInt();
                    id=i;
                    for (Role role : obj) {
                        if (i==role.getId()){
                            a=true;
                        }
                    }
                    if (a){
                        break;
                    }else {
                        continue;
                    }
                }
            }
            System.out.println("请输入姓名");
            String name = scanner.next();
            String password;
            String password1;
            String nickname=" ";
            String truename=" ";
            String phone=" ";
            String email=" ";
            String sex=" ";
            String birthday=" ";
            while (true){
                System.out.println("请输入密码");
                 password = scanner.next();
                System.out.println("请再次输入密码");
                password1 = scanner.next();
                if (password1.equals(password)&&(password1!=null&&password!=null)&&(password1.length()!=0&&password.length()!=0)){

                }else {
                    System.out.println("两次密码不一致  或   密码不能为空" );
                    continue;
                }
                System.out.println("请输入用户名");
                nickname=scanner.next();
                System.out.println("请输入真实姓名");
                truename=scanner.next();
                System.out.println("请输入电话");
                phone=scanner.next();
                System.out.println("请输入邮箱");
                email=scanner.next();
                System.out.println("请输入性别");
                sex=scanner.next();
                System.out.println("请输入生日");
                birthday=scanner.next();
                break;

            }
            User user = new User(0, name, password, id);
            Result result = null;
            if (id==2){
                Teacher teacher = new Teacher(0, 0, nickname, truename, phone, email, sex, birthday,null, null, user,null);
                result=userService.teacherRegister(teacher);
            }else if (id==1){
                Student student = new Student(0, 0, nickname, truename, phone, email, sex, birthday," ",0,user,null);
                result = userService.studentRegister(student);
            }

            if (result.getCode()==200){
                return true;
            }else{
                return false;
            }

            }
        }



    private  void userLogin() {
        while (true){
            System.out.println("请输入用户名");
            String name = scanner.next();
            System.out.println("请输入密码");
            String password = scanner.next();
            System.out.println("请输入是老师登录还是学生登录");
            int roleid=0;
            while (true){
                boolean a=false;
                Result all = roleService.findAll();
                List<Role> obj = (List<Role>) all.getObj();
                for (Role role : obj) {
                    System.out.println("用户类别id:  "+role.getId()+"   用户类别名称:"+role.getName());
                }
                if (all.getCode()==200){
                    System.out.println("请输入想要登录的用户类型");
                    int i = scanner.nextInt();
                    roleid=i;
                    for (Role role : obj) {
                        if (i==role.getId()){
                            a=true;
                        }
                    }
                    if (a){
                        break;
                    }else {
                        break;
                    }
                }
            }
            User user = new User(1, name, password, roleid);
            if (roleid==1){
                Result result = userService.studentLogin(user);
                if ((result.getCode()==200)){
                    Student obj = (Student) result.getObj();
                    AppData.student=obj;
                    User user1 = obj.getUser();
                    AppData.loginUser=user1;
                    new StudentMainView().start();
                    break;
                }

            }else if (roleid==2){
                Result result = userService.teacherLogin(user);
                if ((result.getCode()==200)){
                    Teacher obj = (Teacher) result.getObj();
                    AppData.teacher=obj;
                    AppData.loginUser=obj.getUser();
                    new TeacherMainView().start();
                    break;
                }
            }


        }


    }
}
